package com.pocket.insight.base.mql;

import com.pocket.insight.entity.ME;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/5
 */
public class EvalAdapter {


    public static List<Long> getDatasetInTableFunction(List<ME> exprList) {
        final List<Long> refDatasets = new ArrayList<>();
        exprList.forEach(expr -> (new BaseEval()
        {
            @Override
            public ME traverse(ME me) {
                ME expr = super.traverse(me);
                if (expr.kind == ME.Kind.dataset) {
                    Long datasetId = ME.parseDatasetId(expr.op);
                    if (datasetId != null && !refDatasets.contains(datasetId)) {
                        refDatasets.add(datasetId);
                    }
                }
                return me;
            }
        }).traverse(expr));
        return refDatasets;
    }

    public static ME replaceFieldToRef(ME he, final List<String> fullUids) {
        BaseEval eval = new BaseEval() {
            @Override
            public ME traverse(ME expr) {
                expr = super.traverse(expr);
                if (expr.kind == ME.Kind.field && expr.dataset == null && fullUids.contains(expr.op)) {
                    expr.kind = ME.Kind.reference;
                }
                return expr;
            }
        };
        return eval.traverse(he);
    }

}
